//
//  HWGameWebView.m
//  94hwanGame
//
//  Created by 王治庭 on 2019/3/21.
//  Copyright © 2019 王治庭. All rights reserved.
//

#import "HWGameWebView.h"
#import <WebKit/WebKit.h>
#import "UIColor+Hex.h"
#import "HWHeaderView.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "HWWebViewGameViewController.h"
//#import "HWFirstDetailViewController.h"

@interface HWGameWebView()<WKUIDelegate,WKNavigationDelegate, WKScriptMessageHandler>

@end

@implementation HWGameWebView
{
    WKWebView *_webView;
    NSString *_urlString;
    HWHeaderView *headerView;
    UILabel *title;
    UIButton *backButton;
}

+ (instancetype)initWithUrlString:(NSString *)urlString {
    return [[HWGameWebView alloc] initWithUrlString:urlString];
}

- (instancetype)initWithFrame:(CGRect)frame urlString:(NSString *)urlString {
    _urlString = urlString;
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        
       
        
        
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.userContentController = [[WKUserContentController alloc] init];
        [configuration.userContentController addScriptMessageHandler:self name:@"gameinfo"];
        
       
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        
        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        [request setHTTPMethod:@"GET"];
        [_webView loadRequest:request];
        //
        [self addSubview:_webView];
//        [self webViewDidFinishload:_webView];
        
        
        
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    [super layoutSubviews];
    
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.mas_left);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.right.mas_equalTo(self.mas_right);
    }];
    
}

- (instancetype)initWithUrlString:(NSString *)urlString {
    _urlString = urlString;
    return [self initWithFrame:CGRectZero];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    if ([message.name isEqualToString:@"gameinfo"]) {
        //调用原生扫码
        NSLog(@"返回的数据在这里%@", message.body);
//        HWFirstDetailViewController *detail = [[HWFirstDetailViewController alloc] init];
//        detail.gid = message.body;
//        [[HWViewUtils getCurrentVC].navigationController pushViewController:detail animated:YES];
    }
}





-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"title"]){
        if (object == _webView) {
            title.text = _webView.title;
            self.kongzhiView.navigationItem.title = _webView.title;
            
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    
    if (!navigationAction.targetFrame.isMainFrame) {
        [webView evaluateJavaScript:@"var a = document.getElementsByTagName('a');for(var i=0;i<a.length;i++){a[i].setAttribute('target','');}" completionHandler:nil];
    }
    NSURL *url = navigationAction.request.URL;
    if ([url.absoluteString containsString:@"alipay"] || [url.absoluteString containsString:@"weixin"]) {
        if (url.scheme != nil) {
            if ([url.scheme  isEqual: @"weixin"]) {
                NSLog(@"微信支付");
                [self catchEvent:url];
                decisionHandler(WKNavigationActionPolicyAllow);
                return;
            } else if ([url.scheme isEqualToString:@"alipay"]) {
                NSLog(@"支付宝支付");
                [self catchEvent:url];
                decisionHandler(WKNavigationActionPolicyAllow);
                return;
            }
        }
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)catchEvent:(NSURL *)eventUrl {
    if ([eventUrl.host isEqualToString:@"wap"] || [eventUrl.host isEqualToString:@"alipayclient"]) {
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:eventUrl options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:eventUrl];
        }
    }
}


@end

